package com.jianli.smart.core.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReflectionUtil {
	private static final Logger log = LoggerFactory.getLogger(ReflectionUtil.class);
	
	public static Object newInstance(Class<?> cls) throws RuntimeException{
		Object instance;
		try{
			instance = cls.newInstance();
		}catch(Exception e){
			log.error("instance fail",e);
			throw new RuntimeException(e);
		}
		return instance;
	}
	
	public static Object invokeMethod(Object obj, Method method, Object... args){
		Object result;
		try{
			method.setAccessible(true);
			result = method.invoke(obj, args);
		}catch(Exception e){
			log.error("invoke fail",e);
			throw new RuntimeException(e);
		}
		return result;
	}
	
	public static void setField(Object obj, Field field, Object value){
		try{
			field.setAccessible(true);
			field.set(obj, value);
		}catch(Exception e){
			log.error("invoke fail",e);
			throw new RuntimeException(e);
		}
	}
}
